$(function () {
    home.init();
});
var home = {
    init: function () {
        this.loginAjax();
        this.ajaxForm();
        this.verifyClick();  //验证码点击刷新
        this.ajaxDel();   //ajax 删除
        this.checkAll();  //checkbox 全选和反选
    },
    verifyClick: function () {
        //验证码点击刷新
        var verify = document.getElementById('verify');
        if (verify) {
            var src = verify.src;
            verify.onclick = function () {
                this.src = src + '?v=' + Math.random();
            }
        }

    },
    loginAjax: function () {
        $('.loginAjax').on('submit', function (event) {
            event.preventDefault();
            layer.closeAll();
            if ($("#user_account").val() == '') {
                $('#user_account').addClass('red-border');
                layer.tips('请填写用户名', '#user_account', {tips: [3, '#dd514c'], time: 2000});
            } else if ($("#password").val() == '') {
                $('#password').addClass('red-border');
                layer.tips('请填写密码', '#password', {tips: [3, '#dd514c'], time: 2000});
            } else if ($("#code").val() == '') {
                $('#code').addClass('red-border');
                layer.tips('请填写验证码', '#code', {tips: [3, '#dd514c'], time: 2000});
            } else {
                $.ajax({
                    url: $(this).attr('action'),
                    type: $(this).attr('method'),
                    data: $(this).serialize(),
                    dataType: 'json',
                    success: function (res) {
                        if (res.status) {
                            top.location.href = res.url;
                        } else {
                            layer.msg(res.info);
                            //验证码刷新
                            $('#verify').trigger('click');
                        }
                    },
                    error: function (res) {
                        layer.msg('未知错误');
                    }
                })
            }
        })
    },
    ajaxForm: function () {
        $('.ajaxForm').on('submit', function (event) {
            event.preventDefault();
            var loadIndex = layer.load(1, {shade: false});
            $.ajax({
                url: $(this).attr('action'),
                type: $(this).attr('method'),
                data: $(this).serialize(),
                dataType: 'json',
                success: function (res) {
                    layer.close(loadIndex);
                    if (!res.status) {  //error
                        var color = 'am-alert-danger';
                        var time = '2000';
                    } else {  //success
                        var color = 'am-alert-success';
                        var time = '1500';
                        var fn = function () {
                            if (res.url) {
                                if (parent.layer.getFrameIndex(window.name) != 'undefined') {
                                    layer.close(loadIndex);
                                    parent.location.href = res.url;
                                } else {
                                    location.href = res.url;
                                }
                            } else {
                                if (parent.layer.getFrameIndex(window.name) != 'undefined') {
                                    layer.close(loadIndex);
                                    parent.location.reload(true);
                                } else {
                                    location.reload(true);
                                }
                            }
                        }
                    }
                    home.showInfo(res.info, color, time, fn);
                }
            })

        })
    },

    ajaxDel: function () {
        $('.ajaxDel').on('click', function (event) {
            event.preventDefault();
            var backUrl = $(this).data('backurl');
            var url = $(this).attr('href');
            var msg = $(this).data('msg');
            if (!msg) {
                msg = '你确定要删除吗?';
            }
            var form = $(this).data('form') ? $('#' + $(this).data('form')) : $('form');  //拼接 表单id 默认为$('form')
            home.alertBox(msg, function () {
                layer.load(1, {shade: false});
                $.ajax({
                    url: url,
                    type: 'post',
                    data: form.serialize(),
                    dataType: 'json',
                    success: function (res) {
                        layer.closeAll();
                        if (!res.status) {  //error
                            //var color = 'am-alert-danger';
                            var color = '#dd514c';
                            var time = 2000;
                        } else {  //success
                            //var color = 'am-alert-success';
                            var color = '#5eb95e';
                            var time = 1500;
                            var fn = function () {
                                if (res.url) {
                                    location.href = res.url;
                                } else {
                                    location.reload(true);
                                }
                            }
                        }
                        home.showInfo(res.info,color,time,fn);
                    }
                })
            });
        })
    },

    checkAll: function () {
        $('.checkAll').on('click', function () {
            if ($(this).is(':checked')) {
                $('input[name="ids[]"]').prop('checked', true);
            } else {
                $('input[name="ids[]"]').prop('checked', false);
            }
        })
    },

    alertBox: function (msg, fn) {
        layer.alert(msg, {icon: 3, btn: ['确定', '取消']}, function (index) {
            layer.close(index);
            fn();
        });
    },

    showInfo: function (msg, color, time, fn) {
        var alertHtml = '<div style="position:fixed;top:0;right:0;z-index:1000;width:100%"  class="am-animation-fade am-alert am-alert-pos ' + color + '" data-am-alert>' + '<button type="button" class="am-close">&times;</button>' + '<div class="am-text-center">' + msg + '</div>' + '</div>';
        if ($('.admin-content').length > 0) {
            $('.admin-content').prepend($(alertHtml));
            $('.am-alert').alert();
            var timer = setTimeout(function () {
                $('.am-alert').alert('close');
                if (typeof fn !== 'undefined') {
                    fn();
                }
                clearTimeout(timer);
            }, time);
        } else {
            layer.msg(msg, {icon: 7, time: 2000}, function () {
                (typeof fn !== 'undefined') && fn();
                location.reload();
            });
        }

        return false;

    }
}